package solutions.ferpa.ztest.intf;

import br.com.telescope.adsrt.dom.DomAudit;
import br.com.telescope.adsrt.dom.DomDouble;
import br.com.telescope.adsrt.dom.DomFlag;
import br.com.telescope.adsrt.dom.DomId;
import br.com.telescope.adsrt.dom.DomInteger;
import br.com.telescope.adsrt.dom.DomText;
import br.com.telescope.adsrt.dom.DomTextLong;
import br.com.telescope.adsrt.dom.DomUpper;
import br.com.telescope.adsrt.lib.*;
import br.com.telescope.t2.ServiceModel;
import br.com.telescope.t2.exception.ApplicationRuntimeException;
import br.com.telescope.t2.exception.Severity;
import br.com.telescope.t2.feature.DeleteFeature;
import br.com.telescope.t2.feature.ExportFeature;
import br.com.telescope.t2.feature.ImportFeature;
import br.com.telescope.t2.feature.InsertFeature;
import br.com.telescope.t2.feature.ListFeature;
import br.com.telescope.t2.feature.QueryFeature;
import br.com.telescope.t2.feature.UpdateFeature;
import solutions.ferpa.ztest.ent.*;

/**
 * Cadastro de Preferências.
 * <p>
 * Tela para cadastramento das preferências do sistema. 
 * <P>
 * 
 * <P>
 * As preferências são informações utilizadas pelo sistema para facilitar 
 * a aplicação de seus recursos pelos seus usuários (p.ex, parâmetros de 
 * sistema). 
 * <P>
 * Telescope origin: RTPREF.CAD_PREFERENCIA
 * <P>
 * Base entity: RTPREF.PREFERENCIAS
 * <P>
 * @author TELESCOPE - Build task T2 Web Interface Generator
 * <P>
 * @version 0.1
 */
public class CadPreferencia extends ServiceModel {
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    public static final String GLOBAL_NAME = "RTPREF.CAD_PREFERENCIA";
    
    @Override
    public String globalName() {
        return GLOBAL_NAME;
    }
    
    {
        setTitle("Cadastro de Preferências");
    }
    
    protected EntPreferencias pref;
    protected EntPreferencias rec; // NEED_TJW1_COMPATIBILITY
    
    // Record items
    private DomUpper aPreferencia;
    private DomTextLong aValor;
    private DomTextLong aDescricao;
    private DomUpper aDominio;
    private DomFlag aPorUsuario;
    private DomId aId;
    private DomAudit aAuditoria;
    
    // Query items
    private DomTextLong qDescricao;
    private DomUpper qPreferencia;
    private DomFlag qPorUsuario;
    private DomTextLong qValor;
    
    @Override
    public String getBaseSql() {
        return "select PREF.*"
                + " from PREFERENCIAS PREF";
    }
    
    @Override
    public void initAttributes() {
        
        aPreferencia = new DomUpper();
        aPreferencia.context(ctx).name("PREFERENCIA").label("Preferência").width(60);
        
        aValor = new DomTextLong();
        aValor.context(ctx).name("VALOR").label("Valor").width(60).height(10);
        
        aDescricao = new DomTextLong();
        aDescricao.context(ctx).name("DESCRICAO").label("Descrição").width(60).height(2);
        aDescricao.setDefaultValue("Inserido automaticamente!");
        
        aDominio = new DomUpper();
        aDominio.context(ctx).name("DOMINIO").label("Domínio");
        aDominio.setDefaultValue("ADSRT.TEXT");
        
        aPorUsuario = new DomFlag();
        aPorUsuario.context(ctx).name("POR_USUARIO").label("Por usuário");
        aPorUsuario.setDefaultValue("N");
        
        aId = new DomId();
        aId.context(ctx).name("ID").label("ID");
        
        aAuditoria = new DomAudit();
        aAuditoria.context(ctx).name("AUDITORIA").label("Auditoria").unbound(true);
        
        
        qDescricao = new DomTextLong();
        qDescricao.context(ctx).name("DESCRICAO").label("Descrição");
        
        qPreferencia = new DomUpper();
        qPreferencia.context(ctx).name("PREFERENCIA").label("Preferência");
        
        qPorUsuario = new DomFlag();
        qPorUsuario.context(ctx).name("POR_USUARIO").label("Por usuário");
        
        qValor = new DomTextLong();
        qValor.context(ctx).name("VALOR").label("Valor");
    }
    
    @Override
    public String getQueryPkColumn() {
        return "PREF.ID";
    }
    
    @Override
    public void setId(String value) {
        this.aId.setValue(value);
    }
    
    @Override
    public EntPreferencias getRecord() {
        if (pref == null) {
            pref = new EntPreferencias(ctx);
            rec = pref;
        }
        return this.pref;
    }
    
    @Override
    public String getContextName() {
        return aPreferencia.toText();
    }
    
    @Override
    public String getDefaultOrderBy() {
        return "PREFERENCIA, VALOR, DESCRICAO";
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="QUERY">
    public class FQuery extends QueryFeature {
        
        @Override
        public void initForm() {
            qPreferencia.setDefaultValue("|s");
            addField(qPreferencia, "opt");
            addField(qValor, "opt");
            addField(qDescricao, "opt");
            addField(qPorUsuario, "opt");
        }
        
        {
            setFeatureName("QUERY");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="LIST">
    public class FList extends ListFeature {
        
        @Override
        public void initForm() {
            addField(aPreferencia, "view");
            addField(aValor, "view");
            addField(aDescricao, "view");
        }
        
        {
            setFeatureName("LIST");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="UPDATE">
    public class FUpdate extends UpdateFeature {
        
        @Override
        public void initForm() {
            aPreferencia.setEditable(false);
            addField(aPreferencia, "view");
            addField(aValor, "opt");
            aDescricao.setEditable(false);
            addField(aDescricao, "view");
            aDominio.setEditable(false);
            addField(aDominio, "view");
            aPorUsuario.setEditable(false);
            addField(aPorUsuario, "view");
            aAuditoria.setEditable(false);
            addField(aAuditoria, "view");
        }
        
        @Override
        public void preDisplay() {
            try {
                ajustarDominioValor();
            } catch (AdsException ex) {
                throw new ApplicationRuntimeException(ex);
            }
        }
        
        @Override
        public void onAction() {
            addSubmit(getFeature("UPDATE_2")).label("Avançado");
        }
        
        {
            setFeatureName("UPDATE");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="UPDATE_2">
    public class FUpdate2 extends UpdateFeature {
        
        @Override
        public void initForm() {
            aPreferencia.setOptional(false);
            addField(aPreferencia, "rqd");
            addField(aValor, "opt");
            aDescricao.setOptional(false);
            addField(aDescricao, "rqd");
            aDominio.setOptional(false);
            addField(aDominio, "rqd");
            aPorUsuario.setOptional(false);
            addField(aPorUsuario, "rqd");
        }
        
        {
            setFeatureName("UPDATE_2");
            setTitle("Avançado");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="DELETE">
    public class FDelete extends DeleteFeature {
        
        @Override
        public void initForm() {
            addField(aPreferencia, "view");
        }
        
        {
            setFeatureName("DELETE");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="INSERT">
    public class FInsert extends InsertFeature {
        
        @Override
        public void initForm() {
            aPreferencia.setOptional(false);
            addField(aPreferencia, "rqd");
            addField(aValor, "opt");
            aDescricao.setOptional(false);
            addField(aDescricao, "rqd");
            aDominio.setOptional(false);
            aDominio.setDefaultValue("ADSRT.TEXT");
            addField(aDominio, "rqd");
            aPorUsuario.setOptional(false);
            addField(aPorUsuario, "rqd");
        }
        
        {
            setFeatureName("INSERT");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="EXPORT">
    public class FExport extends ExportFeature {
        
        @Override
        public void initForm() {
            addField(aPreferencia, "view");
            addField(aValor, "view");
            addField(aDescricao, "view");
            addField(aDominio, "view");
            addField(aPorUsuario, "view");
        }
        
        {
            setFeatureName("EXPORT");
            setTitle("Export");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="IMPORT">
    public class FImport extends ImportFeature {
        
        @Override
        public void initForm() {
            addField(aPreferencia, "key");
            addField(aValor, "opt");
            addField(aDescricao, "opt");
            addField(aDominio, "opt");
            aPorUsuario.setOptional(false);
            addField(aPorUsuario, "rqd");
        }
        
        {
            setFeatureName("IMPORT");
            setTitle("Import");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="initFeatures">
    
    @Override
    protected void initFeatures() {
        addFeature(new FQuery());
        addFeature(new FList());
        addFeature(new FUpdate());
        addFeature(new FUpdate2());
        addFeature(new FDelete());
        addFeature(new FInsert());
        addFeature(new FExport());
        addFeature(new FImport());
        super.initFeatures();
    }
    
    @Override
    public String getDefaultFeature() {
        return "QUERY";
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="Getters and Setters">
    
    public String getPreferencia() {
        return aPreferencia.getValue();
    }
    
    public void setPreferencia(String value) {
        this.aPreferencia.setValue(value);
    }
    
    public String getValor() {
        return aValor.getValue();
    }
    
    public void setValor(String value) {
        this.aValor.setValue(value);
    }
    
    public String getDescricao() {
        return aDescricao.getValue();
    }
    
    public void setDescricao(String value) {
        this.aDescricao.setValue(value);
    }
    
    public String getDominio() {
        return aDominio.getValue();
    }
    
    public void setDominio(String value) {
        this.aDominio.setValue(value);
    }
    
    public String getPorUsuario() {
        return aPorUsuario.getValue();
    }
    
    public void setPorUsuario(String value) {
        this.aPorUsuario.setValue(value);
    }
    
    @Override
    public Long getId() {
        return aId.getValue();
    }
    
    public void setId(Long value) {
        this.aId.setValue(value);
    }
    
    public String getAuditoria() {
        return aAuditoria.getValue();
    }
    
    public void setAuditoria(String value) {
        this.aAuditoria.setValue(value);
    }
    public String getQueryPreferencia() {
        return qPreferencia.getQueryValue();
    }
    
    public void setQueryPreferencia(String value) {
        this.qPreferencia.setQueryValue(value);
    }
    public String getQueryValor() {
        return qValor.getQueryValue();
    }
    
    public void setQueryValor(String value) {
        this.qValor.setQueryValue(value);
    }
    public String getQueryDescricao() {
        return qDescricao.getQueryValue();
    }
    
    public void setQueryDescricao(String value) {
        this.qDescricao.setQueryValue(value);
    }
    public String getQueryPorUsuario() {
        return qPorUsuario.getQueryValue();
    }
    
    public void setQueryPorUsuario(String value) {
        this.qPorUsuario.setQueryValue(value);
    }
    // </editor-fold>
    
    /**
     * Ajustar domínio do valor.
     * <p>
     * Antes de apresentar o formulário, ajustar o domínio do valor de acordo 
     * com o que está configurado.
     */
    private void ajustarDominioValor() throws AdsException {
        // <user-code id="878575" parentId="878571" hashcode="-2ad37e3d" length="1021">
        // import br.com.telescope.adsrt.dom.DomText;
        // import br.com.telescope.adsrt.dom.DomInteger;
        // import br.com.telescope.adsrt.dom.DomDouble;
        // import br.com.telescope.t2.exception.Severity;
        String dominio = getRecord().valueOfDominio();
        if (dominio != null && !dominio.isEmpty()) {
            Field field = null;
            String[] parts = dominio.split("\\.");
            String className = AdsGenNames.getJavaClassName(parts[parts.length-1]);
            try {
                Class<Field> fieldClass = (Class<Field>) Class.forName("br.com.telescope.adsrt.dom.Dom" + className);
                field = fieldClass.newInstance();
            } catch (Exception ignore) {
                ctx.pushError(ignore, "", Severity.WARNING, 
                        "Dominio de preferencia invalido (" + dominio + ")");
            }
            if (field != null) {
                field.setName(aValor.getName());
                field.setRequired(aValor.isRequired());
                field.setLabel(aValor.getLabel());
                field.setValue(aValor.getValue());
                getForm().set("VALOR", field);
            }
        }
        // </user-code>
    }
}
